// 同步视频到数据库

const fs = require('fs');
const sequelize = require('../app/db');
const Video = require('../app/models/Video');

// 获取所有视频文件
function getVideoFiles() {
    return new Promise((resolve, reject) => {
        fs.readdir(process.env.VIDEO_PATH, (err, files) => {
            if (err) return reject(err);

            const sqlFiles = files.filter(file => file.endsWith('.mp4'));
            resolve(sqlFiles);
        });
    });
}

(async () => {
    let files = await getVideoFiles();
    files.forEach(filename => {
        sequelize.query(`SELECT * FROM Videos WHERE filename = '${filename}'`).then(res => {
            let row = res[0][0];
            if(!row){
                Video.create({
                    filename
                })
            }
        })
    });
})();